package com.d3works.apps.purchase.web;

import com.d3works.apps.purchase.db.PurchaseOrder;
import com.d3works.apps.purchase.service.PurchaseOrderLineImportService;
import com.d3works.apps.purchase.service.PurchaseOrderService;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.meta.db.MetaFile;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public class PurchaseOrderExtendController {
  protected final Logger logger = LoggerFactory.getLogger(getClass());

  public void importDataLines(ActionRequest request, ActionResponse response) {
    try {
      PurchaseOrder purOrder = request.getContext().asType(PurchaseOrder.class);
      MetaFile metaFile = purOrder.getBindLinesMetaFile();

      PurchaseOrderLineImportService service = new PurchaseOrderLineImportService(metaFile);

      if (service.initialize(metaFile, ",")) {
        purOrder.setPurchaseOrderLineList(service.readAllLines(purOrder));
        purOrder = Beans.get(PurchaseOrderService.class).computePurchaseOrder(purOrder);

        response.setValues(purOrder);
      } else {
        response.setAlert("读取文件失败！");
      }
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }
}
